/**
 * 待办事项模块Mock数据
 */
import Mock from 'mockjs'

// 创建随机数据生成器
const Random = Mock.Random

// 待办类型
const todoTypes = [
  { id: '1', name: '资产申请', icon: 'laptop', color: 'blue' },
  { id: '2', name: '报销审批', icon: 'money-bill-wave', color: 'green' },
  { id: '3', name: '物资申领', icon: 'box', color: 'orange' },
  { id: '4', name: '会议通知', icon: 'calendar-alt', color: 'purple' },
  { id: '5', name: '系统任务', icon: 'tasks', color: 'cyan' }
]

// 待办状态
const todoStatuses = [
  { id: '1', name: '待处理', color: 'orange' },
  { id: '2', name: '处理中', color: 'blue' },
  { id: '3', name: '已完成', color: 'green' },
  { id: '4', name: '已拒绝', color: 'red' },
  { id: '5', name: '已取消', color: 'gray' }
]

// 生成待办事项列表
const generateTodoList = (count = 10) => {
  const todos = []
  
  for (let i = 0; i < count; i++) {
    const typeIndex = Random.integer(0, todoTypes.length - 1)
    const statusIndex = Random.integer(0, todoStatuses.length - 1)
    const type = todoTypes[typeIndex]
    const status = todoStatuses[statusIndex]
    
    // 生成不同类型的待办内容
    let title = ''
    let content = []
    
    switch (type.id) {
      case '1': // 资产申请
        title = '资产' + Random.pick(['领用申请', '归还申请', '维修申请', '采购申请'])
        content = [
          { label: '申请人', value: Random.cname() },
          { label: '资产名称', value: Random.pick(['Dell笔记本电脑', 'iPhone 13 Pro', 'iPad Pro', 'HP打印机']) },
          { label: '申请日期', value: Random.date('yyyy-MM-dd') }
        ]
        break
      case '2': // 报销审批
        title = Random.pick(['差旅费报销', '办公用品报销', '培训费报销', '招待费报销'])
        content = [
          { label: '申请人', value: Random.cname() },
          { label: '报销金额', value: '¥' + Random.float(100, 10000, 2, 2) },
          { label: '申请日期', value: Random.date('yyyy-MM-dd') }
        ]
        break
      case '3': // 物资申领
        title = Random.pick(['办公用品申领', '医疗耗材申领', '实验材料申领', '防护用品申领'])
        content = [
          { label: '申请人', value: Random.cname() },
          { label: '申请部门', value: Random.pick(['行政部', '财务部', '人力资源部', '技术部', '销售部', '医疗部']) },
          { label: '申请日期', value: Random.date('yyyy-MM-dd') }
        ]
        break
      case '4': // 会议通知
        title = Random.pick(['项目启动会', '周例会', '培训会议', '管理层会议'])
        content = [
          { label: '会议时间', value: Random.date('yyyy-MM-dd') + ' ' + Random.time('HH:mm') },
          { label: '会议地点', value: Random.pick(['一号会议室', '二号会议室', '大会议厅', '视频会议室']) },
          { label: '发起人', value: Random.cname() }
        ]
        break
      case '5': // 系统任务
        title = Random.pick(['系统升级通知', '账号安全验证', '周报提交', '绩效考核'])
        content = [
          { label: '截止时间', value: Random.date('yyyy-MM-dd') },
          { label: '重要程度', value: Random.pick(['一般', '重要', '紧急']) },
          { label: '发起部门', value: Random.pick(['系统管理部', '人力资源部', '行政部']) }
        ]
        break
    }
    
    // 生成操作按钮
    let actions = []
    if (status.id === '1') { // 待处理
      if (type.id === '1' || type.id === '2' || type.id === '3') {
        actions = [
          { text: '拒绝', type: 'btn-secondary' },
          { text: '同意', type: 'btn-primary' }
        ]
      } else if (type.id === '4') {
        actions = [
          { text: '不参加', type: 'btn-secondary' },
          { text: '参加', type: 'btn-primary' }
        ]
      } else {
        actions = [
          { text: '忽略', type: 'btn-secondary' },
          { text: '处理', type: 'btn-primary' }
        ]
      }
    } else if (status.id === '2') { // 处理中
      actions = [
        { text: '查看详情', type: 'btn-primary' }
      ]
    } else { // 已完成、已拒绝、已取消
      actions = [
        { text: '查看详情', type: 'btn-secondary' }
      ]
    }
    
    todos.push({
      id: Random.guid(),
      title,
      type,
      status,
      content,
      actions,
      createTime: Random.datetime(),
      updateTime: Random.datetime(),
      isRead: Random.boolean()
    })
  }
  
  return todos
}

// 生成待办统计数据
const generateTodoStats = () => {
  return {
    total: Random.integer(5, 30),
    unread: Random.integer(1, 10),
    pending: Random.integer(1, 15),
    processing: Random.integer(0, 5),
    completed: Random.integer(5, 50),
    rejected: Random.integer(0, 10),
    canceled: Random.integer(0, 5),
    byType: todoTypes.map(type => ({
      type: type.name,
      count: Random.integer(1, 10)
    }))
  }
}

// 模拟API
export function setupTodoMock() {
  // 获取待办事项列表
  Mock.mock(/\/todo\/list/, 'get', (options) => {
    const { url } = options
    // 解析查询参数
    const params = new URLSearchParams(url.split('?')[1])
    const pageSize = parseInt(params.get('pageSize') || '10')
    const currentPage = parseInt(params.get('currentPage') || '1')
    const type = params.get('type')
    const status = params.get('status')
    const keyword = params.get('keyword')
    const isRead = params.get('isRead')
    
    // 生成待办数据
    let data = generateTodoList(30)
    
    // 根据查询条件筛选
    if (type) {
      data = data.filter(item => item.type.id === type)
    }
    if (status) {
      data = data.filter(item => item.status.id === status)
    }
    if (keyword) {
      data = data.filter(item => item.title.includes(keyword))
    }
    if (isRead !== null && isRead !== undefined) {
      const read = isRead === 'true' || isRead === '1'
      data = data.filter(item => item.isRead === read)
    }
    
    // 分页处理
    const total = data.length
    const start = (currentPage - 1) * pageSize
    const end = start + pageSize
    const pageData = data.slice(start, end)
    
    return {
      code: 200,
      message: '操作成功',
      data: {
        list: pageData,
        pagination: {
          total,
          pageSize,
          currentPage
        }
      }
    }
  })
  
  // 获取待办事项详情
  Mock.mock(/\/todo\/detail\/\w+/, 'get', (options) => {
    const id = options.url.split('/').pop()
    
    // 简化的详情数据
    const type = Random.pick(todoTypes)
    const status = Random.pick(todoStatuses)
    
    const detail = {
      id,
      title: type.id === '1' ? '资产申请' : type.id === '2' ? '报销审批' : '会议通知',
      type,
      status,
      content: [
        { label: '申请人', value: Random.cname() },
        { label: '申请日期', value: Random.date('yyyy-MM-dd') },
        { label: '申请事由', value: Random.sentence(5, 20) }
      ],
      processLog: [
        { time: Random.datetime(), operation: '提交申请', operator: Random.cname() }
      ],
      attachments: [],
      createTime: Random.datetime(),
      updateTime: Random.datetime(),
      isRead: true
    }
    
    return {
      code: 200,
      message: '操作成功',
      data: detail
    }
  })
  
  // 获取待办事项统计
  Mock.mock('/todo/stats', 'get', () => {
    return {
      code: 200,
      message: '操作成功',
      data: generateTodoStats()
    }
  })
  
  // 更新待办事项状态
  Mock.mock(/\/todo\/status\/\w+/, 'put', () => {
    return {
      code: 200,
      message: '状态更新成功',
      data: null
    }
  })
  
  // 批准待办事项
  Mock.mock(/\/todo\/approve\/\w+/, 'post', () => {
    return {
      code: 200,
      message: '审批成功',
      data: null
    }
  })
  
  // 拒绝待办事项
  Mock.mock(/\/todo\/reject\/\w+/, 'post', () => {
    return {
      code: 200,
      message: '拒绝成功',
      data: null
    }
  })
  
  // 标记为已读
  Mock.mock(/\/todo\/read\/\w+/, 'put', () => {
    return {
      code: 200,
      message: '标记已读成功',
      data: null
    }
  })
}

export default {
  todoTypes,
  todoStatuses,
  generateTodoList,
  generateTodoStats,
  setupTodoMock
} 